package com.ae.content.web;

import com.sun.jna.*;
import com.sun.jna.win32.*;
import com.sun.jna.platform.win32.*;

import java.util.Arrays;
import java.util.List;

public class WindowsIdleTime {
    public interface Kernel32 extends StdCallLibrary {
        Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
        long GetTickCount();
    }

    public interface User32 extends StdCallLibrary {
        User32 INSTANCE = Native.load("user32", User32.class);
        boolean GetLastInputInfo(LASTINPUTINFO result);
    }

    public static class LASTINPUTINFO extends Structure {
        public WinDef.DWORD cbSize = new WinDef.DWORD(size());
        public WinDef.DWORD dwTime;

        protected List<String> getFieldOrder() {
            return Arrays.asList("cbSize", "dwTime");
        }
    }

    public static long getIdleTimeMillis() {
        LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
        User32.INSTANCE.GetLastInputInfo(lastInputInfo);
        return Kernel32.INSTANCE.GetTickCount() - lastInputInfo.dwTime.longValue();
    }

    public static void main(String[] args) {
        System.out.println("System idle time (ms): " + getIdleTimeMillis());
    }
}

